layui.define(['element', 'layer', 'util', 'form'], function (exports) {
    var $ = layui.jquery;
    var element = layui.element();
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form();

    var index_menu=function(){};

    index_menu.prototype.init=function(){
        var id=4;
        $.post("/srv/rdo.php?", {tpl:"get_left_menu",pid:id}, function (d, e) {
           $("#left_mu").empty();
           $("#left_mu").append("<li class=\"layui-nav-item layui-this\"><a href=\"javascript:void(0);\"><i class=\"fa fa-home\"></i>首页</a></li>");
           $("#left_mu").append(d);
           element.init();
        });
    }  
    
    //监听顶部
    element.on('nav(top_main)', function (elem) {
        var url = $(elem).children('a').attr('data-url');   //页面url
        var id = $(elem).children('a').attr('data-id');     //tab唯一Id
        $.post("/srv/rdo.php?", {tpl:"get_left_menu",pid:id}, function (d, e) {
           $("#left_mu").empty();
           $("#left_mu").append("<li class=\"layui-nav-item layui-this\"><a href=\"javascript:void(0);\"><i class=\"fa fa-home\"></i>首页</a></li>");
           $("#left_mu").append(d);
           element.init();
        });
    });
    //监听左侧导航点击
    element.on('nav(leftnav)', function (elem) {
        var url = $(elem).children('a').attr('data-url');   //页面url
        var id = $(elem).children('a').attr('data-id');     //tab唯一Id
        var title = $(elem).children('a').text();           //菜单名称

        addTb(url,id,title);
    });
    element.on('nav(toprt)',function(elem){
        var url = $(elem).children('a').attr('data-url');   //页面url
        var id = $(elem).children('a').attr('data-id');     //tab唯一Id
        var title = $(elem).children('a').text();           //菜单名称

        addTb(url,id,title);
    });
    function addTb(url,id,title){
        if (title == "首页") {
            element.tabChange('tab', 0);
            return;
        }
        if (url == undefined) return;

        var tabTitleDiv = $('.layui-tab[lay-filter=\'tab\']').children('.layui-tab-title');
        var exist = tabTitleDiv.find('li[lay-id=' + id + ']');
        if (exist.length > 0) {
            //切换到指定索引的卡片
            element.tabChange('tab', id);
        } else {
            var index = layer.load(1);
            //由于Ajax调用本地静态页面存在跨域问题，这里用iframe
            setTimeout(function () {
                //模拟菜单加载
                layer.close(index);
                element.tabAdd('tab', { title: title, content: '<iframe src="' + url + '" style="width:100%;height:100%;border:none;outline:none;"></iframe>', id: id });
                
                /***新增双击刷新****/
                $("[lay-id="+id+"]").bind("dblclick",function(){
                	$(".layui-tab-content").find("[src='"+url+"']").attr("src",url);
                })
                /***************/
                
                //切换到指定索引的卡片
                element.tabChange('tab', id);
            }, 500);
        }
    }
     //收起侧边导航点击事件
    $('.layui-side-hide').click(function(){
        hideSideNav();
    });
    //公告详情
    $('.announce').click(function(){
        var id=$(this).data('id');
        view_dict(id);
    });
    //鼠标靠左展开侧边导航
    $(document).mousemove(function(e){
        if(e.pageX==0){
            showSideNav();
        }
    });
    var ishide=false;
    //隐藏侧边导航
    function hideSideNav(){
        if(!ishide){
            $('.layui-side').animate({left:'-200px'});
            $('.layui-side-hide').animate({left:'-200px'});
            $('.layui-body').animate({left:'0px'});
            $('.layui-footer').animate({left:'0px'});
            ishide=true;
        }
    }
    //显示侧边导航
    function showSideNav() {
        if (ishide) {
            $('.layui-side').animate({ left: '0px' });
            $('.layui-side-hide').animate({ left: '0px' });
            $('.layui-body').animate({ left: '200px' });
            $('.layui-footer').animate({ left: '200px' });
            ishide = false;
        }
    }

    var ind_menu=new index_menu();
     ind_menu.init();

    function view_dict(id) {
        //iframe层
        layer.open({
            type: 2,
            title: '查看公告',
            shadeClose: false,
            shade: 0.8,
            area: ['800px', '600px'],
            full:true,
            content: '?t=announce_view&id='+id
        }); 
    }

    exports('index', ind_menu);
});